
/*   RolGPS Project
 * 
 *   Copyright (C) 2009 Pablo Alejandro Costesich <rhfixer@gmail.com>
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */
package org.rolgps.libedb.engine.sqlite3.zentus;

import java.util.ArrayList;
import org.rolgps.libedb.exception.DuplicateFieldException;
import org.rolgps.libedb.exception.UnknownFieldException;
import org.rolgps.libedb.exception.UnknownPropertyException;
import org.rolgps.libedb.standard.Field;

/**
 *
 * @author xeph
 */
public class Table implements org.rolgps.libedb.standard.Table {

    public static final int INTEGER = 1;
    public static final int DOUBLE = 2;
    public static final int BLOB = 3;
    public static final int STRING = 4;
    public static final String INMEMORYDB = "in memory";
    public static final String DISKDB = "disk";
    public static final String TABLETYPE = "table type";
    private ArrayList<Field> fields = new ArrayList();
    private ArrayList<String[]> properties = new ArrayList();
    private String name = "";
    private boolean trimName = true;

    public Table(String tableName) {
        if (trimName == true) {
            tableName.trim();
        }
        this.name = tableName;
        this.setProperty(Table.TABLETYPE, Table.DISKDB);
    }

    public Table(String tableName, String tableType) {
        if (trimName == true) {
            tableName.trim();
        }
        this.name = tableName;
        this.setProperty(Table.TABLETYPE, tableType);
    }

    public Field[] getFields() {
        return (Field[]) fields.toArray();
    }

    public void deleteField(int id) throws UnknownFieldException {
        if (id >= fields.size() || id < 0) {
            throw new UnknownFieldException("Index out of bounds.");
        } else {
            fields.remove(id);
        }
    }

    public void addField(Field field) throws DuplicateFieldException {
        for (Field f : fields) {
            if (f.getName().equals(field.getName())) {
                throw new DuplicateFieldException();
            }
        }
    }

    public void setProperty(String name, String prop) {
        String[] newField = {name, prop};
        if (!properties.isEmpty()) {
            int iterator = 0;
            for (String[] field : properties) {
                if (field[0].equals(name)) {
                    properties.set(iterator, newField);
                    break;
                }
                iterator++;
            }
        } else {
            properties.add(newField);
        }
    }

    public int getPropertyId(String name) throws UnknownPropertyException {
        int result = -1;
        if (!properties.isEmpty()) {
            int iterator = 0;
            for (String[] field : properties) {
                if (field[0].equals(name)) {
                    result = iterator;
                    break;
                }
                iterator++;
            }
            if (result == -1) {
                throw new UnknownPropertyException();
            }
        } else {
            throw new UnknownPropertyException();
        }
        return result;
    }

    public void deleteProperty(int propId) throws UnknownPropertyException {
        if (propId >= properties.size() || propId < 0) {
            throw new UnknownPropertyException("Index out of bounds.");
        } else {
            properties.remove(propId);
        }
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
